package com.xyh.transaction.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyh.transaction.common.LogLevel;
import com.xyh.transaction.entity.Log;
import com.xyh.transaction.entity.User;
import com.xyh.transaction.entity.dto.log.LogSearchDto;
import com.xyh.transaction.entity.vo.LogLevelVo;
import com.xyh.transaction.entity.vo.LogMethodVo;
import com.xyh.transaction.entity.vo.LogVo;
import com.xyh.transaction.mapper.LogMapper;
import com.xyh.transaction.mapper.UserMapper;
import com.xyh.transaction.service.LogService;
import com.xyh.transaction.utils.MappingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public void save(Log log) {
        logMapper.insert(log);
    }

    @Override
    public PageInfo<LogVo> getList(LogSearchDto logSearchDto) {

        Integer pageNum = logSearchDto.getPageNum();
        Integer pageSize = logSearchDto.getPageSize();

        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }

        List<Log> logList = logMapper.findAll(logSearchDto);

        PageInfo pageInfo = PageInfo.of(logList);

        List<LogVo> logVoList = MappingUtil.mapList(logList, LogVo.class);

        pageInfo.setList(logVoList);

        return pageInfo;
    }

    @Override
    public Set<LogLevelVo> getLevelList() {
        Set<LogLevel> allLogLevel = logMapper.findAllLogLevel();
        return allLogLevel.stream().map(s -> {
            LogLevelVo logLevelVo = new LogLevelVo();
            logLevelVo.setLogLabel(s.name());
            logLevelVo.setLogValue(s.ordinal());
            return logLevelVo;
        }).collect(Collectors.toSet());
    }

    @Override
    public Set<LogMethodVo> getMethodList() {
        Set<String> allMethod = logMapper.findAllMethod();
        return allMethod.stream().map(s -> {
            LogMethodVo logMethodVo = new LogMethodVo();
            logMethodVo.setMethodLabel(s);
            logMethodVo.setMethodValue(s);
            return logMethodVo;
        }).collect(Collectors.toSet());
    }

}
